
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author Klaas Dons
 */
public class SetHighScore implements Runnable{
    private String playerName;
    private int score;
    
    public SetHighScore(String playerName, int score){
        this.playerName = playerName;
        this.score = score;
    }
    
    /**
     * Zorgt ervoor dat de betreffende spelersnaam met zijn score wordt geupload naar een website.
     */
    public void run(){
        String message = "PlayerName=" + this.playerName + "&Score=" + this.score;
        
        //String url = "http://www.klaasdons.nl/j2me/addHighScore.php?PlayerName=" + this.playerName + "&Score=" + Integer.toString(this.score);
        if (this.playerName.length() > 0 && this.score > 0) {
            HttpConnection hc = null;
            OutputStream os = null;

            try {
                hc = (HttpConnection)Connector.open("http://www.klaasdons.nl/j2me/addHighScore.php");
                hc.setRequestMethod(HttpConnection.POST);
                hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                hc.setRequestProperty("Content-Length", Integer.toString(message.length()));

                os = hc.openOutputStream();
                os.write(message.getBytes());
            } catch(IOException e) {
                System.out.println(e.getMessage());
            } finally {
                if (os != null){
                    try {
                        os.close();
                    } catch (IOException e){
                    }
                }

                if (hc != null){
                    try {
                        hc.close();
                    } catch (IOException e){
                    }
                }
            }
        }
    }
}
